package com.zheng.study.base.bean.search.rule;

import com.zheng.study.base.bean.search.SearchEnum;
import com.zheng.study.base.utils.CommonUtil;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - RuleNone
 * ============================================================================
 * author : fallenpanda
 * createDate:  2018/6/11 。
 * ============================================================================
 */
public class RuleNone extends Rule {

	private final String filed;

	public RuleNone(SearchEnum.RuleType ruleType, String filed) {
		super(ruleType);
		this.filed = filed;
	}

	public String getFiled() {
		return filed;
	}

	public static RuleNone isEmpty(String filed){
		return newBuilder().setRuleType(SearchEnum.RuleType.isEmpty).setField(filed).build();
	}

	public static RuleNone isNotEmpty(String filed){
		return newBuilder().setRuleType(SearchEnum.RuleType.isNotEmpty).setField(filed).build();
	}

	public static RuleNone isNull(String filed){
		return newBuilder().setRuleType(SearchEnum.RuleType.isNull).setField(filed).build();
	}

	public static RuleNone isNotNull(String filed){
		return newBuilder().setRuleType(SearchEnum.RuleType.isNotNull).setField(filed).build();
	}

	public static Builder newBuilder(){
		return new Builder();
	}

	public static class Builder extends Rule.Builder<Rule, Builder>{

		private String field;

		@Override
		protected Builder getThis() {
			return this;
		}

		public Builder setField(String field) {
			this.field = field;
			return this;
		}

		@Override
		public RuleNone build() {
			Assert.notNull(ruleType, "ruleType 不能为空");
			Assert.hasText(field, "field 不能为空");
			Assert.isTrue(CommonUtil.isField(field), "field 非法参数");
			return new RuleNone(ruleType, field);
		}
	}

}
